﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class ChucVuBUS
    {
        public void themChucVu(ChucVu cvDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.ChucVus.InsertOnSubmit(cvDTO);

            storeDB.SubmitChanges();
        }

        public void suaChucVu(ChucVu cvDTO, string maChucVu)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.ChucVus.Where(q => q.MaChucVu == maChucVu).First();
            query.TenChucVu = cvDTO.TenChucVu;

            storeDB.SubmitChanges();
        }

        public void xoaChucVu(GridView gridView)
        {
            //Lay MaChucVu duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaChucVu"));

                if (_checkBox.Checked == true)
                {
                    string maChucVuCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maChucVuCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaChucVu duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length>0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));
               
                var query = storeDB.ChucVus.Where(q => q.MaChucVu == maTemp).First();

                storeDB.ChucVus.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachChucVu();
            gridView.DataBind();
        }

        public IList<ChucVu> layDanhSachChucVu()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstChucVu = storeDB.ChucVus.ToList();

            return lstChucVu;
        }

        public Boolean kiemtraTonTaiMaChucVu(string maChucVu)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.ChucVus.Where(q => q.MaChucVu == maChucVu).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenChucVu(string tenChucVu)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.ChucVus.Where(q => q.TenChucVu == tenChucVu).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaChucVu(string maChucVu)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maChucVu = storeDB.ChucVus.Where(m => m.MaChucVu == maChucVu).Select(m =>  m.MaChucVu).First();

            return _maChucVu;
        }

        public string layTenChucVu(string maChucVu)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenChucVu = storeDB.ChucVus.Where(m => m.MaChucVu == maChucVu).Select(m => m.TenChucVu).First();

            return tenChucVu;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.ChucVus.ToList();
            foreach (var q in query)
            {
                var maChucVu = q.MaChucVu;
                var tenChucVu = q.TenChucVu;

                ListItem item = new ListItem();
                item.Text = tenChucVu;
                item.Value = maChucVu;

                dropDownList.Items.Add(item);
            }
        }

        public void sapxepChucVu(DropDownList dropDownList, string maChucVuURL)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var maChucVu = storeDB.ChucVus.Where(m => m.MaChucVu == maChucVuURL).Select(m => m.MaChucVu).First();

            dropDownList.SelectedValue = maChucVu.ToString();
        }

        public string layMaChucVuTheoMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var maChucVu = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.MaChucVu).First();

            return maChucVu;
        }
    }
}
